package org.openinsula.arena.aws.integration.s3;

import javax.annotation.Resource;

import org.jets3t.service.S3ServiceException;
import org.openinsula.arena.aws.AwsConstants;
import org.openinsula.arena.aws.s3.S3Manager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.util.Assert;

/**
 * A ServiceActivator implementation for sending objects to Amazon S3. Returns a
 * {@link String} object, which corresponds to the
 * {@link AwsConstants#S3_RESOURCE_NAME} Header value.
 * 
 * <p>
 * The {@link AwsConstants#S3_BUCKET_NAME} header and the
 * {@link AwsConstants#S3_RESOURCE_NAME} header are required.
 * 
 * @see AwsConstants
 * 
 * @author Edson Yanaga
 */
public class S3SendingService implements InitializingBean {

	@Resource(name = "s3Manager")
	private S3Manager s3Manager;

	private String bucketName;

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(s3Manager, "'s3Manager' must not be null");
		Assert.notNull(bucketName, "'bucketName' must not be null");
	}

	@ServiceActivator
	public String send(@Header(AwsConstants.S3_RESOURCE_NAME) String resourceName, byte[] data)
			throws S3ServiceException {
		s3Manager.put(bucketName, resourceName, data);

		return resourceName;
	}

	public void setS3Manager(S3Manager s3Manager) {
		this.s3Manager = s3Manager;
	}

	public void setBucketName(String bucketName) {
		this.bucketName = bucketName;
	}

}
